package com.elm.dao;

import com.elm.entity.Cart;
import com.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CartDao {
    private final FoodDao foodDao;
    private final BusinessDao businessDao;
    public CartDao() {
        foodDao = new FoodDao();
        businessDao = new BusinessDao();
    }

    public List<Cart> getCartListByUserIdAndBusinessId(String userId, int businessId) throws SQLException {
        List<Cart> carts = new ArrayList<Cart>();
        String sql = "select * from cart where userId=? and businessId=?";

        try(Connection connection = DBUtil.getConnection();
            PreparedStatement ps = connection.prepareStatement(sql)) {
            ps.setString(1, userId);
            ps.setInt(2, businessId);
            ResultSet rs = ps.executeQuery();

            while(rs.next()) {
                Cart cart = new Cart();
                packageCart(carts, rs, cart, businessId);
            }
            return carts;
        }
    }

    public int saveCart(Cart cart) throws SQLException {
        String sql = "insert into cart(foodId, businessId, userId, quantity) values(?,?,?,?)";
        try (Connection connection = DBUtil.getConnection();
             PreparedStatement ps = connection.prepareStatement(sql)){
            ps.setInt(1, cart.getFoodId());
            ps.setInt(2, cart.getBusinessId());
            ps.setString(3, cart.getUserId());
            ps.setInt(4, cart.getQuantity());
            return ps.executeUpdate();
        }catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    public int updateCart(Cart cart) {
        String sql = "UPDATE cart SET quantity = ? WHERE businessId = ? AND userId = ? AND foodId = ?";
        try (Connection connection = DBUtil.getConnection();
             PreparedStatement ps = connection.prepareStatement(sql)) {
            ps.setInt(1, cart.getQuantity());
            ps.setInt(2, cart.getBusinessId());
            ps.setString(3, cart.getUserId());
            ps.setInt(4, cart.getFoodId());
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    public int removeCart(Cart cart) throws SQLException {
        String sql = "DELETE FROM cart WHERE businessId = ? AND userId = ? AND foodId = ?";
        try(Connection connection = DBUtil.getConnection();
            PreparedStatement ps = connection.prepareStatement(sql)) {
            ps.setInt(1, cart.getBusinessId());
            ps.setString(2, cart.getUserId());
            ps.setInt(3, cart.getFoodId());
            return ps.executeUpdate();
        }catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    public List<Cart> getCartListByUserId(String userId) {
        String sql = "select * from cart where userId = ?";
        List<Cart> carts = new ArrayList<>();
        try(Connection connection = DBUtil.getConnection();
            PreparedStatement ps = connection.prepareStatement(sql)) {
            ps.setString(1, userId);
            ResultSet rs = ps.executeQuery();
            while(rs.next()) {
                Cart cart = new Cart();
                int businessId = rs.getInt("businessId");
                packageCart(carts, rs, cart, businessId);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return carts;
    }

    public int getCartQuantity(String userId, int businessId,int foodId) throws SQLException {
        String sql = "select quantity from cart where userId = ? and businessId = ? and foodId = ?";
        try(Connection connection = DBUtil.getConnection();
            PreparedStatement ps = connection.prepareStatement(sql)) {
            ps.setString(1, userId);
            ps.setInt(2, businessId);
            ps.setInt(3, foodId);
            return ps.executeQuery().getInt("quantity");
        }catch (SQLException e) {
            throw e;
        }
    }

    private void packageCart(List<Cart> carts, ResultSet rs, Cart cart, int businessId) throws SQLException {
        int foodId = rs.getInt("foodId");
        cart.setUserId(rs.getString("userId"));
        cart.setCartId(rs.getInt("cartId"));
        cart.setFoodId(foodId);
        cart.setQuantity(rs.getInt("quantity"));
        cart.setFood(foodDao.getFoodById(foodId));
        cart.setBusinessId(businessId);
        cart.setBusiness(businessDao.getBusinessById(businessId));
        carts.add(cart);
    }
}